import {type} from "./type";

export * from './type';
export * from './localStorage';
export * from './config';
export * from './IdCode';

//深拷贝
export const deepClone = (trg) => {
    let copyed_objs = [];
    function _deepCopy(target) {
        // if ((typeof target !== 'object') || !target || target instanceof Date) {
        //     return target;
        // }
        const tarType = type(target);
        if( tarType !== 'object' || !target || tarType === 'date'){
            return target;
        }
        for (let i = 0; i < copyed_objs.length; i++) {
            if (copyed_objs[i].target === target) {
                return copyed_objs[i].copyTarget;
            }
        }
        let obj = {};
        if (tarType === 'array') {
            obj = []; // 处理target是数组的情况
        }
        copyed_objs.push({target: target, copyTarget: obj});
        Object.keys(target).forEach(key => {
            if (obj[key]) {
                return;
            }
            obj[key] = _deepCopy(target[key]);
        });
        return obj;
    }
    return _deepCopy(trg);
};


//新生成formdata
export const newFormData = (data = {}) => {
    const formdata = new FormData();
    for( let key in data){
        formdata.append(key , data[key]);
    }
    return formdata;
};


//公用的timeout定时器 参数：( callback ， time )
export const timeout = (callback , time) => {
    time = Number(time) || 100;
    callback = type(callback) === 'function' ? callback : null;
    if (callback) {
        let newTimeout = null;
        newTimeout = window.setTimeout(function () {
            callback();
            window.clearTimeout(newTimeout);
            newTimeout = null;
            time = null;
            callback = null;
        }, time)
    }
};

//随机数 用于dom节点唯一的id/class/ref生成
export const newMathRandom = ()=> Math.floor(Math.random()*(1-10000) + 10000);



//对象转数组
export const objToArr = (obj , keyName = 'key', valueName = 'value')=>{
    let arr = [];
    for(let key in obj){
        arr.push({
            [keyName] : key ,
            [valueName] : obj[key]
        });
    }
    return arr;
};

//获取地址参数
export const getUrlQuery = name  => {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(window.location.href) || [, ""])[1].replace(/\+/g, '%20')) || null;
};

export const login2Url = param => {
    const  redirect  = window.location.search.replace(/\?redirect\=/,'');
    if(!redirect){
        return;
    }
    let temp;
    if(redirect.indexOf('?') !== -1){
        temp = redirect + `&access_token=${param}`
    }else{
        temp = redirect + `?access_token=${param}`
    }
    window.location.href = temp
}
/**
 * 下载文件函数
 * @param {string} url
 * @param {string} fileName
 */
export function download(url, fileName = '') {
    url = /^http|https|location|data:/.test(url) ? url : window.location.origin + url;
    const a = document.createElement('a'); // 创建a标签
    a.setAttribute('download', fileName); // download属性
    a.setAttribute('href', url); // href链接
    a.click(); // 自执行点击事件
}
